package com.woniuxy.lrz.carlproject.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniuxy.lrz.carlproject.common.AppException;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsPropertyValueAOUForm;
import com.woniuxy.lrz.carlproject.controller.form.formgoods.GoodsPropertyValueFindForm;
import com.woniuxy.lrz.carlproject.model.GoodPropertyValue;
import com.woniuxy.lrz.carlproject.dao.GoodPropertyValueMapper;
import com.woniuxy.lrz.carlproject.service.GoodPropertyValueService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.lrz.carlproject.service.dto.dtogoods.GoodsPropertyValueDto;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * <p>
 * 属性值表 服务实现类
 * </p>
 *
 * @author lrz
 * @since 2022-09-28
 */
@Service
public class GoodPropertyValueServiceImpl extends ServiceImpl<GoodPropertyValueMapper, GoodPropertyValue> implements GoodPropertyValueService {
    @Resource
    private GoodPropertyValueMapper goodPropertyValueMapper;

    @Override
    public boolean addGoodProperty(GoodsPropertyValueAOUForm goodsPropertyValueAOUForms) {
            GoodPropertyValue goodPropertyValue = BeanUtil.copyProperties(goodsPropertyValueAOUForms, GoodPropertyValue.class);
            GoodPropertyValue propert_value = goodPropertyValueMapper.selectOne(new QueryWrapper<GoodPropertyValue>().eq("propert_value", goodsPropertyValueAOUForms.getPropertValue()));
            //验证新增还是修改
            if(goodsPropertyValueAOUForms.getPropertyValueId()==null){
               //新增
                //验证新增的内容是否重复
                if(propert_value!=null&&propert_value.getPropertyId()==goodPropertyValue.getPropertyId()){
                    throw new AppException("同一属性下有重名的值，请稍后再试！");
                }
                goodPropertyValueMapper.insert(goodPropertyValue);
            }
            //修改
            //验证修改后的内容是否重名
            if(propert_value!=null&&propert_value.getPropertyId()==goodPropertyValue.getPropertyId()){
                throw new AppException("同一属性下有重名的值，请稍后再试！");
            }
            goodPropertyValueMapper.updateById(goodPropertyValue);
        return true;
    }

    @Override
    public boolean deleteGoodPropertyValue(Integer propertyValueId) {
        //验证是否重复删除
        GoodPropertyValue goodPropertyValue = goodPropertyValueMapper.selectById(propertyValueId);
        if(goodPropertyValue==null) throw new AppException("请勿重复操作！");
        goodPropertyValueMapper.deleteById(propertyValueId);
        return true;
    }

    @Override
    public Page<GoodsPropertyValueDto> findGoodPropertyValue(GoodsPropertyValueFindForm goodsPropertyValueFindForm) {
        Page<GoodPropertyValue> page=new Page<>(goodsPropertyValueFindForm.getPageNum(),goodsPropertyValueFindForm.getPageSize());
        Page<GoodsPropertyValueDto> goodPropertyValue = goodPropertyValueMapper.findGoodPropertyValue(page, goodsPropertyValueFindForm);
        return goodPropertyValue;
    }


}
